{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-08-22T06:16:49.133348Z",
     "start_time": "2024-08-22T06:16:48.885494Z"
    }
   },
   "source": [
    "# 导包\n",
    "import pandas as pd\n",
    "\n",
    "from python.day3.demo10_关联 import sum_score\n",
    "from python.day5.demo9_继承 import student"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:19:05.818984Z",
     "start_time": "2024-08-22T06:19:05.781213Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1、Series ；代表一个列\n",
    "ser = pd.Series(data=[1, 2, 3, 4, 5], index=[\"a\", \"b\", \"c\", \"d\", \"e\"])\n",
    "ser"
   ],
   "id": "7a3cddb3d7d1e2e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "d    4\n",
       "e    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:19:30.083970Z",
     "start_time": "2024-08-22T06:19:30.068406Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过下标获取值\n",
    "ser[\"a\"]"
   ],
   "id": "c1e9cc1c32f71000",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:20:11.665260Z",
     "start_time": "2024-08-22T06:20:11.649633Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 布尔值索引\n",
    "ser[ser > 2]"
   ],
   "id": "26580e4a3461c702",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "c    3\n",
       "d    4\n",
       "e    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:20:58.672751Z",
     "start_time": "2024-08-22T06:20:58.657122Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取索引\n",
    "ser.index"
   ],
   "id": "3934d041b5183615",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c', 'd', 'e'], dtype='object')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:21:26.887030Z",
     "start_time": "2024-08-22T06:21:26.871275Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取值\n",
    "ser.values"
   ],
   "id": "1cc7dae877a51819",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5], dtype=int64)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:35:38.069556Z",
     "start_time": "2024-08-22T06:35:38.042914Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2、DataFrame,表结构，有行有列\n",
    "# 创建DF\n",
    "df = pd.DataFrame(data={\n",
    "    \"name\": [\"张三\", \"李四\", \"王五\", \"赵六\"],\n",
    "    \"age\": [23, 24, 25, 26]\n",
    "})\n",
    "df"
   ],
   "id": "8a3bbf34b23419e9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  name  age\n",
       "0   张三   23\n",
       "1   李四   24\n",
       "2   王五   25\n",
       "3   赵六   26"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>张三</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>李四</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>王五</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>赵六</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:27:19.354832Z",
     "start_time": "2024-08-22T06:27:19.339088Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过列名获取列\n",
    "df[\"name\"]"
   ],
   "id": "a278e21e0496e649",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    张三\n",
       "1    李四\n",
       "2    王五\n",
       "3    赵六\n",
       "Name: name, dtype: object"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:29:10.527430Z",
     "start_time": "2024-08-22T06:29:10.512530Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#E 布尔值索引\n",
    "df[df[\"age\"] > 23]"
   ],
   "id": "28304c78362293ce",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  name  age\n",
       "1   李四   24\n",
       "2   王五   25\n",
       "3   赵六   26"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>李四</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>王五</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>赵六</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:33:06.198073Z",
     "start_time": "2024-08-22T06:33:06.182443Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 删除列,默认返回新的DF\n",
    "# inplace： 当inplace等于True时是在源DF中做处理\n",
    "df.drop(\"name\", axis=1, inplace=True)"
   ],
   "id": "bb02a9396b5386e9",
   "outputs": [],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:34:05.757560Z",
     "start_time": "2024-08-22T06:34:05.741903Z"
    }
   },
   "cell_type": "code",
   "source": "df",
   "id": "d53fc78c4ddc64",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   age\n",
       "0   23\n",
       "1   24\n",
       "2   25\n",
       "3   26"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:34:38.801122Z",
     "start_time": "2024-08-22T06:34:38.785496Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 直接删除\n",
    "del df[\"age\"]\n",
    "df"
   ],
   "id": "954ffe691aa2daa6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: [0, 1, 2, 3]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:36:02.978654Z",
     "start_time": "2024-08-22T06:36:02.963025Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 删除某一行\n",
    "df.drop(2, axis=0)"
   ],
   "id": "251a5bcb1a2c40ff",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  name  age\n",
       "0   张三   23\n",
       "1   李四   24\n",
       "3   赵六   26"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>张三</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>李四</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>赵六</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:42:24.387691Z",
     "start_time": "2024-08-22T06:42:24.372048Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 读取文件\n",
    "students = pd.read_csv(\"../../data/student.txt\",  # 读取文件的路径\n",
    "                       sep=\",\",  # 数据分隔符\n",
    "                       header=None,  # 如果文件第一行不是列名需要设置为None\n",
    "                       names=[\"id\", \"name\", \"age\", \"sex\", \"clazz\"],  # 指定列名\n",
    "                       encoding=\"utf-8\"\n",
    "                       )\n",
    "# 打印前4行\n",
    "students.head(5)"
   ],
   "id": "aff10e2b5b498e87",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "           id name  age sex clazz\n",
       "0  1500100001  施笑槐   22   女  文科六班\n",
       "1  1500100002  吕金鹏   24   男  文科六班\n",
       "2  1500100003  单乐蕊   22   女  理科六班\n",
       "3  1500100004  葛德曜   24   男  理科三班\n",
       "4  1500100005  宣谷芹   22   女  理科五班"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>clazz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>施笑槐</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1500100002</td>\n",
       "      <td>吕金鹏</td>\n",
       "      <td>24</td>\n",
       "      <td>男</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1500100003</td>\n",
       "      <td>单乐蕊</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1500100004</td>\n",
       "      <td>葛德曜</td>\n",
       "      <td>24</td>\n",
       "      <td>男</td>\n",
       "      <td>理科三班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1500100005</td>\n",
       "      <td>宣谷芹</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>理科五班</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:43:08.230612Z",
     "start_time": "2024-08-22T06:43:08.199366Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取性别为男的学生\n",
    "students[students[\"sex\"] == \"男\"]"
   ],
   "id": "a0e8cb492ec420db",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             id name  age sex clazz\n",
       "1    1500100002  吕金鹏   24   男  文科六班\n",
       "3    1500100004  葛德曜   24   男  理科三班\n",
       "5    1500100006  边昂雄   21   男  理科二班\n",
       "8    1500100009  沈德昌   21   男  理科一班\n",
       "9    1500100010  羿彦昌   23   男  理科六班\n",
       "..          ...  ...  ...  ..   ...\n",
       "991  1500100992  莫运盛   24   男  理科六班\n",
       "995  1500100996  厉运凡   24   男  文科三班\n",
       "996  1500100997  陶敬曦   21   男  理科六班\n",
       "997  1500100998  容昆宇   22   男  理科四班\n",
       "999  1500101000  符瑞渊   23   男  理科六班\n",
       "\n",
       "[507 rows x 5 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>clazz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1500100002</td>\n",
       "      <td>吕金鹏</td>\n",
       "      <td>24</td>\n",
       "      <td>男</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1500100004</td>\n",
       "      <td>葛德曜</td>\n",
       "      <td>24</td>\n",
       "      <td>男</td>\n",
       "      <td>理科三班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1500100006</td>\n",
       "      <td>边昂雄</td>\n",
       "      <td>21</td>\n",
       "      <td>男</td>\n",
       "      <td>理科二班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1500100009</td>\n",
       "      <td>沈德昌</td>\n",
       "      <td>21</td>\n",
       "      <td>男</td>\n",
       "      <td>理科一班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1500100010</td>\n",
       "      <td>羿彦昌</td>\n",
       "      <td>23</td>\n",
       "      <td>男</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>991</th>\n",
       "      <td>1500100992</td>\n",
       "      <td>莫运盛</td>\n",
       "      <td>24</td>\n",
       "      <td>男</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>1500100996</td>\n",
       "      <td>厉运凡</td>\n",
       "      <td>24</td>\n",
       "      <td>男</td>\n",
       "      <td>文科三班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>1500100997</td>\n",
       "      <td>陶敬曦</td>\n",
       "      <td>21</td>\n",
       "      <td>男</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>1500100998</td>\n",
       "      <td>容昆宇</td>\n",
       "      <td>22</td>\n",
       "      <td>男</td>\n",
       "      <td>理科四班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>1500101000</td>\n",
       "      <td>符瑞渊</td>\n",
       "      <td>23</td>\n",
       "      <td>男</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>507 rows × 5 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:46:34.344690Z",
     "start_time": "2024-08-22T06:46:34.329079Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计班级学生的人数（安装班级分组，统计数量）\n",
    "# groupby:分组，将相同的分到同一个组内\n",
    "students.groupby(\"clazz\")[\"clazz\"].count()"
   ],
   "id": "609b414307bffd71",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "clazz\n",
       "文科一班     72\n",
       "文科三班     94\n",
       "文科二班     87\n",
       "文科五班     84\n",
       "文科六班    104\n",
       "文科四班     81\n",
       "理科一班     78\n",
       "理科三班     68\n",
       "理科二班     79\n",
       "理科五班     70\n",
       "理科六班     92\n",
       "理科四班     91\n",
       "Name: clazz, dtype: int64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:47:20.753021Z",
     "start_time": "2024-08-22T06:47:20.721638Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# value_counts: 统计值的数量\n",
    "students[\"clazz\"].value_counts()"
   ],
   "id": "a0c13ec44c139290",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "文科六班    104\n",
       "文科三班     94\n",
       "理科六班     92\n",
       "理科四班     91\n",
       "文科二班     87\n",
       "文科五班     84\n",
       "文科四班     81\n",
       "理科二班     79\n",
       "理科一班     78\n",
       "文科一班     72\n",
       "理科五班     70\n",
       "理科三班     68\n",
       "Name: clazz, dtype: int64"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:54:54.691522Z",
     "start_time": "2024-08-22T06:54:54.660277Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计每个班级性别为男的学生的平均年龄\n",
    "clazz_avg_age = students[students[\"sex\"] == \"男\"].groupby(\"clazz\").mean()[\"age\"]\n",
    "clazz_avg_age"
   ],
   "id": "dad730734e669ebb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "clazz\n",
       "文科一班    22.387097\n",
       "文科三班    22.640000\n",
       "文科二班    22.367347\n",
       "文科五班    22.302326\n",
       "文科六班    22.581818\n",
       "文科四班    22.743590\n",
       "理科一班    22.387097\n",
       "理科三班    22.828571\n",
       "理科二班    22.472222\n",
       "理科五班    22.636364\n",
       "理科六班    22.454545\n",
       "理科四班    22.680000\n",
       "Name: age, dtype: float64"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T06:57:51.251700Z",
     "start_time": "2024-08-22T06:57:51.220445Z"
    }
   },
   "cell_type": "code",
   "source": "clazz_avg_age.to_csv(\"../../data/clazz_avg_age.txt\", encoding=\"utf-8\", header=None)",
   "id": "e5d2775a432b6066",
   "outputs": [],
   "execution_count": 54
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T07:39:58.593702Z",
     "start_time": "2024-08-22T07:39:58.562330Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 读取分数表\n",
    "scores = pd.read_csv(\"../../data/score.txt\",\n",
    "                     sep=\",\",\n",
    "                     header=None,\n",
    "                     names=[\"id\", \"cid\", \"score\"]\n",
    "                     )\n",
    "scores"
   ],
   "id": "cf0ce92d89eb40bf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "              id      cid  score\n",
       "0     1500100001  1000001     98\n",
       "1     1500100001  1000002      5\n",
       "2     1500100001  1000003    137\n",
       "3     1500100001  1000004     29\n",
       "4     1500100001  1000005     85\n",
       "...          ...      ...    ...\n",
       "5995  1500101000  1000002     78\n",
       "5996  1500101000  1000003     81\n",
       "5997  1500101000  1000007      5\n",
       "5998  1500101000  1000008     87\n",
       "5999  1500101000  1000009     28\n",
       "\n",
       "[6000 rows x 3 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>cid</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>1000001</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>1000002</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>1000003</td>\n",
       "      <td>137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>1000004</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>1000005</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5995</th>\n",
       "      <td>1500101000</td>\n",
       "      <td>1000002</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5996</th>\n",
       "      <td>1500101000</td>\n",
       "      <td>1000003</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5997</th>\n",
       "      <td>1500101000</td>\n",
       "      <td>1000007</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5998</th>\n",
       "      <td>1500101000</td>\n",
       "      <td>1000008</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5999</th>\n",
       "      <td>1500101000</td>\n",
       "      <td>1000009</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6000 rows × 3 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T07:40:19.880866Z",
     "start_time": "2024-08-22T07:40:19.865286Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计学生总分\n",
    "scores.groupby(\"id\")[\"score\"].sum()"
   ],
   "id": "eed11c77efeb8952",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "id\n",
       "1500100001    406\n",
       "1500100002    440\n",
       "1500100003    359\n",
       "1500100004    421\n",
       "1500100005    395\n",
       "             ... \n",
       "1500100996    355\n",
       "1500100997    293\n",
       "1500100998    398\n",
       "1500100999    371\n",
       "1500101000    379\n",
       "Name: score, Length: 1000, dtype: int64"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T07:41:43.547557Z",
     "start_time": "2024-08-22T07:41:43.516311Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 标准差\n",
    "scores.groupby(\"id\")[\"score\"].std()"
   ],
   "id": "694b81bff4051fd",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "id\n",
       "1500100001    48.363898\n",
       "1500100002    44.978513\n",
       "1500100003    46.481896\n",
       "1500100004    44.291835\n",
       "1500100005    32.908459\n",
       "                ...    \n",
       "1500100996    46.948553\n",
       "1500100997    32.547913\n",
       "1500100998    38.520990\n",
       "1500100999    39.493881\n",
       "1500101000    37.637304\n",
       "Name: score, Length: 1000, dtype: float64"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 60
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T07:42:38.102209Z",
     "start_time": "2024-08-22T07:42:38.086599Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 总分计算\n",
    "scores[\"score\"].mean()"
   ],
   "id": "34eb96ec31647576",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "62.117333333333335"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T07:44:26.410176Z",
     "start_time": "2024-08-22T07:44:26.394579Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计学生的总分\n",
    "sum_score = scores.groupby(\"id\")[\"score\"].sum()\n",
    "sum_score"
   ],
   "id": "30e0b29543dd0db5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "id\n",
       "1500100001    406\n",
       "1500100002    440\n",
       "1500100003    359\n",
       "1500100004    421\n",
       "1500100005    395\n",
       "             ... \n",
       "1500100996    355\n",
       "1500100997    293\n",
       "1500100998    398\n",
       "1500100999    371\n",
       "1500101000    379\n",
       "Name: score, Length: 1000, dtype: int64"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T07:48:19.182148Z",
     "start_time": "2024-08-22T07:48:19.150868Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计每个班级总分的平均分\n",
    "\n",
    "# 关联学生表和分数表\n",
    "merge = students.merge(scores, on=\"id\")\n",
    "merge"
   ],
   "id": "11f3e6404802c5e8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "              id name  age sex clazz      cid  score\n",
       "0     1500100001  施笑槐   22   女  文科六班  1000001     98\n",
       "1     1500100001  施笑槐   22   女  文科六班  1000002      5\n",
       "2     1500100001  施笑槐   22   女  文科六班  1000003    137\n",
       "3     1500100001  施笑槐   22   女  文科六班  1000004     29\n",
       "4     1500100001  施笑槐   22   女  文科六班  1000005     85\n",
       "...          ...  ...  ...  ..   ...      ...    ...\n",
       "5995  1500101000  符瑞渊   23   男  理科六班  1000002     78\n",
       "5996  1500101000  符瑞渊   23   男  理科六班  1000003     81\n",
       "5997  1500101000  符瑞渊   23   男  理科六班  1000007      5\n",
       "5998  1500101000  符瑞渊   23   男  理科六班  1000008     87\n",
       "5999  1500101000  符瑞渊   23   男  理科六班  1000009     28\n",
       "\n",
       "[6000 rows x 7 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>clazz</th>\n",
       "      <th>cid</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>施笑槐</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "      <td>1000001</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>施笑槐</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "      <td>1000002</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>施笑槐</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "      <td>1000003</td>\n",
       "      <td>137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>施笑槐</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "      <td>1000004</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>施笑槐</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "      <td>1000005</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5995</th>\n",
       "      <td>1500101000</td>\n",
       "      <td>符瑞渊</td>\n",
       "      <td>23</td>\n",
       "      <td>男</td>\n",
       "      <td>理科六班</td>\n",
       "      <td>1000002</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5996</th>\n",
       "      <td>1500101000</td>\n",
       "      <td>符瑞渊</td>\n",
       "      <td>23</td>\n",
       "      <td>男</td>\n",
       "      <td>理科六班</td>\n",
       "      <td>1000003</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5997</th>\n",
       "      <td>1500101000</td>\n",
       "      <td>符瑞渊</td>\n",
       "      <td>23</td>\n",
       "      <td>男</td>\n",
       "      <td>理科六班</td>\n",
       "      <td>1000007</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5998</th>\n",
       "      <td>1500101000</td>\n",
       "      <td>符瑞渊</td>\n",
       "      <td>23</td>\n",
       "      <td>男</td>\n",
       "      <td>理科六班</td>\n",
       "      <td>1000008</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5999</th>\n",
       "      <td>1500101000</td>\n",
       "      <td>符瑞渊</td>\n",
       "      <td>23</td>\n",
       "      <td>男</td>\n",
       "      <td>理科六班</td>\n",
       "      <td>1000009</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6000 rows × 7 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T07:53:44.271568Z",
     "start_time": "2024-08-22T07:53:44.255944Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算每个学生的总分\n",
    "sum_score = merge.groupby([\"id\", \"clazz\"])[\"score\"].sum()\n",
    "print(sum_score, type(sum_score))"
   ],
   "id": "82969add68abd70d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id          clazz\n",
      "1500100001  文科六班     406\n",
      "1500100002  文科六班     440\n",
      "1500100003  理科六班     359\n",
      "1500100004  理科三班     421\n",
      "1500100005  理科五班     395\n",
      "                    ... \n",
      "1500100996  文科三班     355\n",
      "1500100997  理科六班     293\n",
      "1500100998  理科四班     398\n",
      "1500100999  文科五班     371\n",
      "1500101000  理科六班     379\n",
      "Name: score, Length: 1000, dtype: int64 <class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "execution_count": 79
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T07:54:40.200578Z",
     "start_time": "2024-08-22T07:54:40.184957Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# reset_index:重置索引\n",
    "sum_score.reset_index().groupby(\"clazz\")[\"score\"].mean()"
   ],
   "id": "3a3581fe33165d72",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "clazz\n",
       "文科一班    377.944444\n",
       "文科三班    383.670213\n",
       "文科二班    368.896552\n",
       "文科五班    365.404762\n",
       "文科六班    370.240385\n",
       "文科四班    380.234568\n",
       "理科一班    356.679487\n",
       "理科三班    376.632353\n",
       "理科二班    378.721519\n",
       "理科五班    386.885714\n",
       "理科六班    365.163043\n",
       "理科四班    366.010989\n",
       "Name: score, dtype: float64"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 80
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T07:56:45.360184Z",
     "start_time": "2024-08-22T07:56:45.328813Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计每个班级的平均分\n",
    "students \\\n",
    "    .merge(scores, on=\"id\") \\\n",
    "    .groupby([\"id\", \"clazz\"])[\"score\"].sum() \\\n",
    "    .reset_index() \\\n",
    "    .groupby(\"clazz\")[\"score\"].mean()"
   ],
   "id": "4966c43f4af80242",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "clazz\n",
       "文科一班    377.944444\n",
       "文科三班    383.670213\n",
       "文科二班    368.896552\n",
       "文科五班    365.404762\n",
       "文科六班    370.240385\n",
       "文科四班    380.234568\n",
       "理科一班    356.679487\n",
       "理科三班    376.632353\n",
       "理科二班    378.721519\n",
       "理科五班    386.885714\n",
       "理科六班    365.163043\n",
       "理科四班    366.010989\n",
       "Name: score, dtype: float64"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 85
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:01:43.162876Z",
     "start_time": "2024-08-22T08:01:43.126801Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计单词的数量\n",
    "\n",
    "# 读取数据\n",
    "lines = pd.read_csv(\"../../data/lines.txt\",\n",
    "                    sep=\"\\t\",\n",
    "                    header=None,\n",
    "                    names=[\"line\"]\n",
    "                    )\n",
    "lines"
   ],
   "id": "2190bfecda379a74",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                                                 line\n",
       "0                             java,python,hadoop,hive\n",
       "1                                    java,hadoop,hive\n",
       "2                                  java,python,hadoop\n",
       "3   java,python,hadoop,hive,hadoop,hive,python,python\n",
       "4                             java,python,hadoop,hive\n",
       "5               java,python,hadoop,hive,python,python\n",
       "6                                  java,python,hadoop\n",
       "7   java,python,hadoop,hive,hadoop,hive,python,python\n",
       "8                             java,python,hadoop,hive\n",
       "9   java,python,hadoop,hive,hadoop,hive,python,python\n",
       "10                            java,python,hadoop,hive\n",
       "11         java,python,hadoop,hive,hadoop,hive,python\n",
       "12                                        java,python\n",
       "13  java,python,hadoop,hive,hadoop,hive,python,python\n",
       "14                            java,python,hadoop,hive\n",
       "15  java,python,hadoop,hive,hadoop,hive,python,python\n",
       "16                            java,python,hadoop,hive\n",
       "17                            java,python,hadoop,hive\n",
       "18         java,hadoop,hive,hadoop,hive,python,python\n",
       "19                            java,python,hadoop,hive\n",
       "20  java,python,hadoop,hive,hadoop,hive,python,python\n",
       "21                                   java,hadoop,hive\n",
       "22                            java,python,hadoop,hive\n",
       "23                            java,python,hadoop,hive\n",
       "24  java,python,hadoop,hive,hadoop,hive,python,python\n",
       "25                            java,python,hadoop,hive\n",
       "26                                   java,python,hive\n",
       "27                            java,python,hadoop,hive\n",
       "28  java,python,hadoop,hive,hadoop,hive,python,python\n",
       "29                            java,python,hadoop,hive"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>line</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>java,python,hadoop,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>java,hadoop,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>java,python,hadoop</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>java,python,hadoop,hive,hadoop,hive,python,python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>java,python,hadoop,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>java,python,hadoop,hive,python,python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>java,python,hadoop</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>java,python,hadoop,hive,hadoop,hive,python,python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>java,python,hadoop,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>java,python,hadoop,hive,hadoop,hive,python,python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>java,python,hadoop,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>java,python,hadoop,hive,hadoop,hive,python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>java,python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>java,python,hadoop,hive,hadoop,hive,python,python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>java,python,hadoop,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>java,python,hadoop,hive,hadoop,hive,python,python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>java,python,hadoop,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>java,python,hadoop,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>java,hadoop,hive,hadoop,hive,python,python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>java,python,hadoop,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>java,python,hadoop,hive,hadoop,hive,python,python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>java,hadoop,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>java,python,hadoop,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>java,python,hadoop,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>java,python,hadoop,hive,hadoop,hive,python,python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>java,python,hadoop,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>java,python,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>java,python,hadoop,hive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>java,python,hadoop,hive,hadoop,hive,python,python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>java,python,hadoop,hive</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 87
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:06:23.842898Z",
     "start_time": "2024-08-22T08:06:23.827253Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# split: 对列值进行切分，expand：将切分得到的列表转换成多列\n",
    "# stack# 将一行中多列转换成多行\n",
    "lines[\"line\"].str.split(\",\", expand=True).stack().value_counts()"
   ],
   "id": "ce96b76fa3c5a754",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "python    48\n",
       "hadoop    38\n",
       "hive      37\n",
       "java      30\n",
       "dtype: int64"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 94
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:07:51.171293Z",
     "start_time": "2024-08-22T08:07:51.155670Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取前N行\n",
    "students.head(2)"
   ],
   "id": "bd8e9ae911579e24",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "           id name  age sex clazz\n",
       "0  1500100001  施笑槐   22   女  文科六班\n",
       "1  1500100002  吕金鹏   24   男  文科六班"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>clazz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>施笑槐</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1500100002</td>\n",
       "      <td>吕金鹏</td>\n",
       "      <td>24</td>\n",
       "      <td>男</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 95
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:08:00.363023Z",
     "start_time": "2024-08-22T08:08:00.347264Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取后n行\n",
    "students.tail(2)"
   ],
   "id": "b462f50d97455b39",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             id name  age sex clazz\n",
       "998  1500100999  钟绮晴   23   女  文科五班\n",
       "999  1500101000  符瑞渊   23   男  理科六班"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>clazz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>1500100999</td>\n",
       "      <td>钟绮晴</td>\n",
       "      <td>23</td>\n",
       "      <td>女</td>\n",
       "      <td>文科五班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>1500101000</td>\n",
       "      <td>符瑞渊</td>\n",
       "      <td>23</td>\n",
       "      <td>男</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 96
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:08:33.287451Z",
     "start_time": "2024-08-22T08:08:33.271825Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取行数和列数量\n",
    "students.shape"
   ],
   "id": "f6442934fa16c6ab",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1000, 5)"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 99
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:08:55.960486Z",
     "start_time": "2024-08-22T08:08:55.944859Z"
    }
   },
   "cell_type": "code",
   "source": "len(students)",
   "id": "19660a6459646282",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 100
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:09:24.679006Z",
     "start_time": "2024-08-22T08:09:24.643778Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看基本信息\n",
    "students.info()"
   ],
   "id": "7037d8bbd3c705ff",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1000 entries, 0 to 999\n",
      "Data columns (total 5 columns):\n",
      " #   Column  Non-Null Count  Dtype \n",
      "---  ------  --------------  ----- \n",
      " 0   id      1000 non-null   int64 \n",
      " 1   name    1000 non-null   object\n",
      " 2   age     1000 non-null   int64 \n",
      " 3   sex     1000 non-null   object\n",
      " 4   clazz   1000 non-null   object\n",
      "dtypes: int64(2), object(3)\n",
      "memory usage: 39.2+ KB\n"
     ]
    }
   ],
   "execution_count": 101
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:11:45.212068Z",
     "start_time": "2024-08-22T08:11:45.196600Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计数值类型的信息\n",
    "students.describe()"
   ],
   "id": "8ad2065f8796106",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                 id          age\n",
       "count  1.000000e+03  1000.000000\n",
       "mean   1.500101e+09    22.521000\n",
       "std    2.888194e+02     1.113013\n",
       "min    1.500100e+09    21.000000\n",
       "25%    1.500100e+09    22.000000\n",
       "50%    1.500101e+09    22.000000\n",
       "75%    1.500101e+09    24.000000\n",
       "max    1.500101e+09    24.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1.000000e+03</td>\n",
       "      <td>1000.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.500101e+09</td>\n",
       "      <td>22.521000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>2.888194e+02</td>\n",
       "      <td>1.113013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.500100e+09</td>\n",
       "      <td>21.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.500100e+09</td>\n",
       "      <td>22.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.500101e+09</td>\n",
       "      <td>22.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>1.500101e+09</td>\n",
       "      <td>24.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.500101e+09</td>\n",
       "      <td>24.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 102
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:18:15.518613Z",
     "start_time": "2024-08-22T08:18:15.496477Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 定义函数对对列值做处理\n",
    "def fun(c):\n",
    "    if c.name == \"clazz\":\n",
    "        # 截取文理科\n",
    "        return c.str[0:2]\n",
    "    else:\n",
    "        return c\n",
    "\n",
    "# 取出文理科\n",
    "students.apply(fun)"
   ],
   "id": "8e9b50bfe945c7ea",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             id name  age sex clazz\n",
       "0    1500100001  施笑槐   22   女    文科\n",
       "1    1500100002  吕金鹏   24   男    文科\n",
       "2    1500100003  单乐蕊   22   女    理科\n",
       "3    1500100004  葛德曜   24   男    理科\n",
       "4    1500100005  宣谷芹   22   女    理科\n",
       "..          ...  ...  ...  ..   ...\n",
       "995  1500100996  厉运凡   24   男    文科\n",
       "996  1500100997  陶敬曦   21   男    理科\n",
       "997  1500100998  容昆宇   22   男    理科\n",
       "998  1500100999  钟绮晴   23   女    文科\n",
       "999  1500101000  符瑞渊   23   男    理科\n",
       "\n",
       "[1000 rows x 5 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>clazz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>施笑槐</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>文科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1500100002</td>\n",
       "      <td>吕金鹏</td>\n",
       "      <td>24</td>\n",
       "      <td>男</td>\n",
       "      <td>文科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1500100003</td>\n",
       "      <td>单乐蕊</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>理科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1500100004</td>\n",
       "      <td>葛德曜</td>\n",
       "      <td>24</td>\n",
       "      <td>男</td>\n",
       "      <td>理科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1500100005</td>\n",
       "      <td>宣谷芹</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>理科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>1500100996</td>\n",
       "      <td>厉运凡</td>\n",
       "      <td>24</td>\n",
       "      <td>男</td>\n",
       "      <td>文科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>1500100997</td>\n",
       "      <td>陶敬曦</td>\n",
       "      <td>21</td>\n",
       "      <td>男</td>\n",
       "      <td>理科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>1500100998</td>\n",
       "      <td>容昆宇</td>\n",
       "      <td>22</td>\n",
       "      <td>男</td>\n",
       "      <td>理科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>1500100999</td>\n",
       "      <td>钟绮晴</td>\n",
       "      <td>23</td>\n",
       "      <td>女</td>\n",
       "      <td>文科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>1500101000</td>\n",
       "      <td>符瑞渊</td>\n",
       "      <td>23</td>\n",
       "      <td>男</td>\n",
       "      <td>理科</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 5 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 109
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:20:29.043924Z",
     "start_time": "2024-08-22T08:20:29.012653Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对数据进行处理，增加新的列\n",
    "students[\"type\"] = students[\"clazz\"].str[0:2]\n",
    "students"
   ],
   "id": "1695a0dd1d710761",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             id name  age sex clazz type\n",
       "0    1500100001  施笑槐   22   女  文科六班   文科\n",
       "1    1500100002  吕金鹏   24   男  文科六班   文科\n",
       "2    1500100003  单乐蕊   22   女  理科六班   理科\n",
       "3    1500100004  葛德曜   24   男  理科三班   理科\n",
       "4    1500100005  宣谷芹   22   女  理科五班   理科\n",
       "..          ...  ...  ...  ..   ...  ...\n",
       "995  1500100996  厉运凡   24   男  文科三班   文科\n",
       "996  1500100997  陶敬曦   21   男  理科六班   理科\n",
       "997  1500100998  容昆宇   22   男  理科四班   理科\n",
       "998  1500100999  钟绮晴   23   女  文科五班   文科\n",
       "999  1500101000  符瑞渊   23   男  理科六班   理科\n",
       "\n",
       "[1000 rows x 6 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>clazz</th>\n",
       "      <th>type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>施笑槐</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "      <td>文科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1500100002</td>\n",
       "      <td>吕金鹏</td>\n",
       "      <td>24</td>\n",
       "      <td>男</td>\n",
       "      <td>文科六班</td>\n",
       "      <td>文科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1500100003</td>\n",
       "      <td>单乐蕊</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>理科六班</td>\n",
       "      <td>理科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1500100004</td>\n",
       "      <td>葛德曜</td>\n",
       "      <td>24</td>\n",
       "      <td>男</td>\n",
       "      <td>理科三班</td>\n",
       "      <td>理科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1500100005</td>\n",
       "      <td>宣谷芹</td>\n",
       "      <td>22</td>\n",
       "      <td>女</td>\n",
       "      <td>理科五班</td>\n",
       "      <td>理科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>1500100996</td>\n",
       "      <td>厉运凡</td>\n",
       "      <td>24</td>\n",
       "      <td>男</td>\n",
       "      <td>文科三班</td>\n",
       "      <td>文科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>1500100997</td>\n",
       "      <td>陶敬曦</td>\n",
       "      <td>21</td>\n",
       "      <td>男</td>\n",
       "      <td>理科六班</td>\n",
       "      <td>理科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>1500100998</td>\n",
       "      <td>容昆宇</td>\n",
       "      <td>22</td>\n",
       "      <td>男</td>\n",
       "      <td>理科四班</td>\n",
       "      <td>理科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>1500100999</td>\n",
       "      <td>钟绮晴</td>\n",
       "      <td>23</td>\n",
       "      <td>女</td>\n",
       "      <td>文科五班</td>\n",
       "      <td>文科</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>1500101000</td>\n",
       "      <td>符瑞渊</td>\n",
       "      <td>23</td>\n",
       "      <td>男</td>\n",
       "      <td>理科六班</td>\n",
       "      <td>理科</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 6 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 112
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:51:40.963372Z",
     "start_time": "2024-08-22T08:51:40.947753Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数据清洗\n",
    "\n",
    "student_na = pd.read_csv(\"../../data/student_na.txt\",\n",
    "            sep=\",\",\n",
    "            header=None,\n",
    "            names=[\"id\",\"name\",\"age\",\"sex\",\"clazz\"],\n",
    "            encoding=\"utf-8\"\n",
    "            )\n",
    "student_na"
   ],
   "id": "eb39031ebcf905f4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "           id name   age  sex clazz\n",
       "0  1500100001  施笑槐  22.0    女  文科六班\n",
       "1  1500100002  吕金鹏  24.0  NaN  文科六班\n",
       "2  1500100003  单乐蕊  22.0    女  理科六班\n",
       "3  1500100004  葛德曜   NaN    男  理科三班\n",
       "4  1500100005  宣谷芹  22.0    女  理科五班\n",
       "5  1500100006  边昂雄  21.0    男  理科二班\n",
       "6  1500100007  尚孤风   NaN    女  文科六班\n",
       "7  1500100008  符半双  22.0    女  理科六班\n",
       "8  1500100009  沈德昌  21.0    男  理科一班"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>clazz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>施笑槐</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1500100002</td>\n",
       "      <td>吕金鹏</td>\n",
       "      <td>24.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1500100003</td>\n",
       "      <td>单乐蕊</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1500100004</td>\n",
       "      <td>葛德曜</td>\n",
       "      <td>NaN</td>\n",
       "      <td>男</td>\n",
       "      <td>理科三班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1500100005</td>\n",
       "      <td>宣谷芹</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>理科五班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1500100006</td>\n",
       "      <td>边昂雄</td>\n",
       "      <td>21.0</td>\n",
       "      <td>男</td>\n",
       "      <td>理科二班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1500100007</td>\n",
       "      <td>尚孤风</td>\n",
       "      <td>NaN</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1500100008</td>\n",
       "      <td>符半双</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1500100009</td>\n",
       "      <td>沈德昌</td>\n",
       "      <td>21.0</td>\n",
       "      <td>男</td>\n",
       "      <td>理科一班</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 127
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:46:47.056397Z",
     "start_time": "2024-08-22T08:46:47.040769Z"
    }
   },
   "cell_type": "code",
   "source": "student_na.isna()",
   "id": "f7983ac9c7ab96ba",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      id   name    age    sex  clazz\n",
       "0  False  False  False  False  False\n",
       "1  False  False  False   True  False\n",
       "2  False  False  False  False  False\n",
       "3  False  False   True  False  False\n",
       "4  False  False  False  False  False\n",
       "5  False  False  False  False  False\n",
       "6  False  False   True  False  False\n",
       "7  False  False  False  False  False\n",
       "8  False  False  False  False  False"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>clazz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 114
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:47:28.236808Z",
     "start_time": "2024-08-22T08:47:28.221195Z"
    }
   },
   "cell_type": "code",
   "source": "student_na.notna()",
   "id": "7363e4d282eca5a4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     id  name    age    sex  clazz\n",
       "0  True  True   True   True   True\n",
       "1  True  True   True  False   True\n",
       "2  True  True   True   True   True\n",
       "3  True  True  False   True   True\n",
       "4  True  True   True   True   True\n",
       "5  True  True   True   True   True\n",
       "6  True  True  False   True   True\n",
       "7  True  True   True   True   True\n",
       "8  True  True   True   True   True"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>clazz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 117
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:47:58.559614Z",
     "start_time": "2024-08-22T08:47:58.537483Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 删除存在空的行\n",
    "student_na.dropna()"
   ],
   "id": "681263f56ed11506",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "           id name   age sex clazz\n",
       "0  1500100001  施笑槐  22.0   女  文科六班\n",
       "2  1500100003  单乐蕊  22.0   女  理科六班\n",
       "4  1500100005  宣谷芹  22.0   女  理科五班\n",
       "5  1500100006  边昂雄  21.0   男  理科二班\n",
       "7  1500100008  符半双  22.0   女  理科六班\n",
       "8  1500100009  沈德昌  21.0   男  理科一班"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>clazz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>施笑槐</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1500100003</td>\n",
       "      <td>单乐蕊</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1500100005</td>\n",
       "      <td>宣谷芹</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>理科五班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1500100006</td>\n",
       "      <td>边昂雄</td>\n",
       "      <td>21.0</td>\n",
       "      <td>男</td>\n",
       "      <td>理科二班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1500100008</td>\n",
       "      <td>符半双</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1500100009</td>\n",
       "      <td>沈德昌</td>\n",
       "      <td>21.0</td>\n",
       "      <td>男</td>\n",
       "      <td>理科一班</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 118
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:48:54.457729Z",
     "start_time": "2024-08-22T08:48:54.442110Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 删除存在空的列\n",
    "student_na.dropna(axis=1)"
   ],
   "id": "ba826d3d25ac8ea4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "           id name clazz\n",
       "0  1500100001  施笑槐  文科六班\n",
       "1  1500100002  吕金鹏  文科六班\n",
       "2  1500100003  单乐蕊  理科六班\n",
       "3  1500100004  葛德曜  理科三班\n",
       "4  1500100005  宣谷芹  理科五班\n",
       "5  1500100006  边昂雄  理科二班\n",
       "6  1500100007  尚孤风  文科六班\n",
       "7  1500100008  符半双  理科六班\n",
       "8  1500100009  沈德昌  理科一班"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>clazz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>施笑槐</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1500100002</td>\n",
       "      <td>吕金鹏</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1500100003</td>\n",
       "      <td>单乐蕊</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1500100004</td>\n",
       "      <td>葛德曜</td>\n",
       "      <td>理科三班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1500100005</td>\n",
       "      <td>宣谷芹</td>\n",
       "      <td>理科五班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1500100006</td>\n",
       "      <td>边昂雄</td>\n",
       "      <td>理科二班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1500100007</td>\n",
       "      <td>尚孤风</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1500100008</td>\n",
       "      <td>符半双</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1500100009</td>\n",
       "      <td>沈德昌</td>\n",
       "      <td>理科一班</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 120
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:49:16.672073Z",
     "start_time": "2024-08-22T08:49:16.649931Z"
    }
   },
   "cell_type": "code",
   "source": "student_na",
   "id": "c5f952f79baa89fe",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "           id name   age  sex clazz\n",
       "0  1500100001  施笑槐  22.0    女  文科六班\n",
       "1  1500100002  吕金鹏  24.0  NaN  文科六班\n",
       "2  1500100003  单乐蕊  22.0    女  理科六班\n",
       "3  1500100004  葛德曜   NaN    男  理科三班\n",
       "4  1500100005  宣谷芹  22.0    女  理科五班\n",
       "5  1500100006  边昂雄  21.0    男  理科二班\n",
       "6  1500100007  尚孤风   NaN    女  文科六班\n",
       "7  1500100008  符半双  22.0    女  理科六班\n",
       "8  1500100009  沈德昌  21.0    男  理科一班"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>clazz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>施笑槐</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1500100002</td>\n",
       "      <td>吕金鹏</td>\n",
       "      <td>24.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1500100003</td>\n",
       "      <td>单乐蕊</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1500100004</td>\n",
       "      <td>葛德曜</td>\n",
       "      <td>NaN</td>\n",
       "      <td>男</td>\n",
       "      <td>理科三班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1500100005</td>\n",
       "      <td>宣谷芹</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>理科五班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1500100006</td>\n",
       "      <td>边昂雄</td>\n",
       "      <td>21.0</td>\n",
       "      <td>男</td>\n",
       "      <td>理科二班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1500100007</td>\n",
       "      <td>尚孤风</td>\n",
       "      <td>NaN</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1500100008</td>\n",
       "      <td>符半双</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1500100009</td>\n",
       "      <td>沈德昌</td>\n",
       "      <td>21.0</td>\n",
       "      <td>男</td>\n",
       "      <td>理科一班</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 121
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:51:52.825524Z",
     "start_time": "2024-08-22T08:51:52.807403Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 填充固定值\n",
    "student_na[\"sex\"] = student_na[\"sex\"].fillna(\"其它\")"
   ],
   "id": "4eac813ce5bd5b0d",
   "outputs": [],
   "execution_count": 128
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:51:53.505684Z",
     "start_time": "2024-08-22T08:51:53.490061Z"
    }
   },
   "cell_type": "code",
   "source": "student_na",
   "id": "1127dc0cdae53492",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "           id name   age sex clazz\n",
       "0  1500100001  施笑槐  22.0   女  文科六班\n",
       "1  1500100002  吕金鹏  24.0  其它  文科六班\n",
       "2  1500100003  单乐蕊  22.0   女  理科六班\n",
       "3  1500100004  葛德曜   NaN   男  理科三班\n",
       "4  1500100005  宣谷芹  22.0   女  理科五班\n",
       "5  1500100006  边昂雄  21.0   男  理科二班\n",
       "6  1500100007  尚孤风   NaN   女  文科六班\n",
       "7  1500100008  符半双  22.0   女  理科六班\n",
       "8  1500100009  沈德昌  21.0   男  理科一班"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>clazz</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1500100001</td>\n",
       "      <td>施笑槐</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1500100002</td>\n",
       "      <td>吕金鹏</td>\n",
       "      <td>24.0</td>\n",
       "      <td>其它</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1500100003</td>\n",
       "      <td>单乐蕊</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1500100004</td>\n",
       "      <td>葛德曜</td>\n",
       "      <td>NaN</td>\n",
       "      <td>男</td>\n",
       "      <td>理科三班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1500100005</td>\n",
       "      <td>宣谷芹</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>理科五班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1500100006</td>\n",
       "      <td>边昂雄</td>\n",
       "      <td>21.0</td>\n",
       "      <td>男</td>\n",
       "      <td>理科二班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1500100007</td>\n",
       "      <td>尚孤风</td>\n",
       "      <td>NaN</td>\n",
       "      <td>女</td>\n",
       "      <td>文科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1500100008</td>\n",
       "      <td>符半双</td>\n",
       "      <td>22.0</td>\n",
       "      <td>女</td>\n",
       "      <td>理科六班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1500100009</td>\n",
       "      <td>沈德昌</td>\n",
       "      <td>21.0</td>\n",
       "      <td>男</td>\n",
       "      <td>理科一班</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 129
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-22T08:52:58.358823Z",
     "start_time": "2024-08-22T08:52:58.343193Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 填充统计值，均值，最大值，最小值\n",
    "student_na[\"age\"].fillna(student_na[\"age\"].mean())"
   ],
   "id": "aaee2edc7fae5211",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    22.0\n",
       "1    24.0\n",
       "2    22.0\n",
       "3    22.0\n",
       "4    22.0\n",
       "5    21.0\n",
       "6    22.0\n",
       "7    22.0\n",
       "8    21.0\n",
       "Name: age, dtype: float64"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 133
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "7fa59ba45345e62a"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
